package com.rcloud.server.sealtalk.controller;

import com.rcloud.server.sealtalk.controller.param.ExpressQueryParam;
import com.rcloud.server.sealtalk.exception.ServiceException;
import com.rcloud.server.sealtalk.manager.ExpressManager;
import com.rcloud.server.sealtalk.model.response.APIResult;
import com.rcloud.server.sealtalk.model.response.APIResultWrap;
import com.rcloud.server.sealtalk.service.AttachFileService;
import com.rcloud.server.sealtalk.util.ValidateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author: zc
 * @date: 2021/6/12
 * @description: 快递查询控制器
 */
@Slf4j
@Api(tags = "快递相关接口")
@RestController
@RequestMapping("/express")
public class ExpressInfoController extends BaseController {
    @Autowired
    private ExpressManager expressManager;

    @Autowired
    private AttachFileService attachFileService;

    /**
     * 快递单号识别进行查询的功能
     */
    @ApiOperation(value = "快递单号识别进行查询的功能")
    @RequestMapping(value = "/queryExpress", method = RequestMethod.POST)
    public APIResult<Object> queryExpress(@RequestBody ExpressQueryParam param) throws ServiceException {
        String com = param.getCom();
        String num = param.getNum();
        ValidateUtils.notEmpty(num);
        Integer currentUserId = getCurrentUserId();
        return APIResultWrap.ok(expressManager.queryExpress(currentUserId, com, num));
    }

    /**
     * 条码识别快递查询
     */
    @PostMapping(value = "/queryQrCodeReg")
    @ApiOperation(value = "条码识别查询", notes = "条码识别查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "图片文件", dataType = "MultipartFile")
    })
    public APIResult<Object> queryQrCodeReg(@RequestParam("file") MultipartFile file) throws ServiceException, IOException {
        String fileName = attachFileService.uploadFile(file.getBytes(), file.getOriginalFilename());
        String barCoder = "http://" + sealtalkConfig.getQiniuBucketDomain() + "/" + fileName;
        String qrCodeReg = expressManager.queryQrCodeReg(barCoder);
        return APIResultWrap.ok(qrCodeReg);
    }


}
